/*
* $Id: ViewFamilies.cpp 153 2010-09-26 17:33:07Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "ViewFamilies.h"
#include "Settings/Journal/FamilyViewSettings.h"
#include "Settings/DinIntDefined.h"
#include "Settings/SettingControlIds.h"
#include "Components/Journal/QueenBeeManager.h"
#include "Components/Journal/InspectManager.h"
#include "Components/Journal/FamilyManager.h"
#include "ViewFieldValuesHelper.h"
#include "Event/EventId.h"

#pragma warning(disable: 4407)

CViewFamilies::CViewFamilies(CState* pState)
: CViewTable(pState){
	IEventManager::GetEventManager()->SubscribeEvent(this, FAMILY_STN_CHANGE_EVENT);
	IEventManager::GetEventManager()->SubscribeEvent(this, QUEEN_BEE_DB_CHANGE_EVENT);
	IEventManager::GetEventManager()->SubscribeEvent(this, JOURNAL_DB_CHANGE_EVENT);
}
CViewFamilies::~CViewFamilies(){
	IEventManager::GetEventManager()->UnSubscribeEvent(this, FAMILY_STN_CHANGE_EVENT);
	IEventManager::GetEventManager()->UnSubscribeEvent(this, QUEEN_BEE_DB_CHANGE_EVENT);
	IEventManager::GetEventManager()->UnSubscribeEvent(this, JOURNAL_DB_CHANGE_EVENT);
}

bool CViewFamilies::IsViewState(State state){
	return state.m_nPosTab == Journal::INSPECT_JOURNAL &&
		state.m_type == Journal::NONE &&
		state.m_nTypeSel == Journal::POINT;
}

void CViewFamilies::SetState(){
	m_pState->m_nState = Journal::VIEW_FAMILY;
	Init();
}

void CViewFamilies::Init(){
	m_tableRegion.GetTableDrawer()->ResetTable();
	tableHeader *nameHeader = NULL, *typeHeader = NULL, *noteHeader = NULL;
	nameHeader = m_tableRegion.GetTableDrawer()->AddHeader(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_FAMILY_NAME_ID), TABLE_FAMILY_NAME_ID, TypeField::STRING);
	CFamilyViewSettings pSettings;
	if(pSettings.GetState(CHECK_TYPE_ID) == CHECK_BOX_STATE_1)
		typeHeader = m_tableRegion.GetTableDrawer()->AddHeader(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_FAMILY_TYPE_ID), TABLE_FAMILY_TYPE_ID, TypeField::STRING);
	if(pSettings.GetState(CHECK_NOTE_ID) == CHECK_BOX_STATE_1)
		noteHeader = m_tableRegion.GetTableDrawer()->AddHeader(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_NOTE_ID), TABLE_FAMILY_NOTE_ID, TypeField::STRING);
	CViewFieldValuesHelper helper(m_tableRegion.GetTableDrawer());
	helper.GenerateUserHeaders(TypeField::FAMILIES, TABLE_FAMILY_USER_ID);
	tableHeader *strengthHeader = NULL, *isQueenBeeHeader = NULL, *honeyHeader = NULL,	*broodHeader = NULL,
		*housingHeader = NULL, *woxHeader = NULL, *frameHeader = NULL, *birthdayQueenBeeHeader = NULL;
	if(pSettings.GetState(CHECK_FRAME_ID) == CHECK_BOX_STATE_1)
		frameHeader = m_tableRegion.GetTableDrawer()->AddHeader(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_FRAME_ID), TABLE_FAMILY_FRAME_ID, TypeField::INTEGER);
	if(pSettings.GetState(CHECK_BEES_ID) == CHECK_BOX_STATE_1)
		strengthHeader= m_tableRegion.GetTableDrawer()->AddHeader(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_BEES_ID), TABLE_FAMILY_BEES_ID, TypeField::INTEGER);
	if(pSettings.GetState(CHECK_HONEY_ID) == CHECK_BOX_STATE_1)
		honeyHeader = m_tableRegion.GetTableDrawer()->AddHeader(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_HONEY_ID), TABLE_FAMILY_HONEY_ID, TypeField::INTEGER);
	if(pSettings.GetState(CHECK_BROOD_ID) == CHECK_BOX_STATE_1)
		broodHeader = m_tableRegion.GetTableDrawer()->AddHeader(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_BROOD_ID), TABLE_FAMILY_BROOD_ID, TypeField::INTEGER);
	if(pSettings.GetState(CHECK_WOX_ID) == CHECK_BOX_STATE_1)
		woxHeader = m_tableRegion.GetTableDrawer()->AddHeader(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_WAX_ID), TABLE_FAMILY_WOX_ID, TypeField::INTEGER);
	if(pSettings.GetState(CHECK_HOUSING_ID) == CHECK_BOX_STATE_1)
		housingHeader = m_tableRegion.GetTableDrawer()->AddHeader(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_HOUSING_ID), TABLE_FAMILY_HOUSING_ID, TypeField::INTEGER);
	if(pSettings.GetState(CHECK_EXIST_BEE_QUEEN_ID) == CHECK_BOX_STATE_1)
		isQueenBeeHeader = m_tableRegion.GetTableDrawer()->AddHeader(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_EBQ_ID), TABLE_FAMILY_EBQ_ID, TypeField::BOOL);
	if(pSettings.GetState(CHECK_BIRTHDAY_BEE_QUEEN_ID) == CHECK_BOX_STATE_1)
		birthdayQueenBeeHeader = m_tableRegion.GetTableDrawer()->AddHeader(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_BBQ_ID), TABLE_FAMILY_BBQ_ID, TypeField::DATE);
	m_tableRegion.GetTableDrawer()->NormalizeHeader();

	CFamilyManager* pFamilyManager = IJournalComponent::GetFamilyManager();
	CInspectManager* pInspectManager = IJournalComponent::GetInspectManager();
	std::map<int, CFamilyManager::family> aFamilies = pFamilyManager->GetFamilies(m_pState->m_nIdParameter);
	for(std::map<int, CFamilyManager::family>::iterator it1 = aFamilies.begin(); it1 != aFamilies.end(); it1++){
		CInspectManager::lastInspect lastIns = pInspectManager->GetFamilyLastJournal(it1->first);
		CTableCelRow *row = new CTableCelRow(it1->first, Journal::VIEW_FAMILY);
		row->AddCel(nameHeader, it1->second.m_strName);
		if(noteHeader)
			row->AddCel(noteHeader, it1->second.m_strNote);
		if(typeHeader)
			row->AddCel(typeHeader, CCommonFunction::GetStringType(it1->second.m_type));
		std::tstring strFrame, strStrength, strHoney, strBrood, strWox, strHousing, strIsQueenBee;
		strFrame = CConvertFunction::ConvertFromInt(lastIns.m_nFrame);
		strHoney = CConvertFunction::ConvertFromInt(lastIns.m_nHoney);
		strBrood = CConvertFunction::ConvertFromInt(lastIns.m_nBrood);
		strStrength = CConvertFunction::ConvertFromInt(lastIns.m_nStrength);
		strWox = CConvertFunction::ConvertFromInt(lastIns.m_nWax);
		strHousing = CConvertFunction::ConvertFromInt(lastIns.m_nHousing);
		strIsQueenBee = ILanguageManager::GetLanguageManager()->GetString(QUEEN_BEE_STATUS_DEFAULT_ID + lastIns.m_nQBState);
		std::tstring strBirthDayQB;
		std::map<int, CQueenBeeManager::queen_bee> aQb = IJournalComponent::GetQueenBeeManager()->GetFamilyQB(it1->first);
		for(std::map<int, CQueenBeeManager::queen_bee>::iterator it = aQb.begin(); it != aQb.end(); it++){
			if(!it->second.s_bDead)
				strBirthDayQB += CConvertFunction::ConvertFromDate(it->second.s_date) + _T(" ");
		}
		if(frameHeader)
			row->AddCel(frameHeader, strFrame);
		if(strengthHeader)
			row->AddCel(strengthHeader, strStrength);
		if(honeyHeader)
			row->AddCel(honeyHeader, strHoney);
		if(broodHeader)
			row->AddCel(broodHeader, strBrood);
		if(woxHeader)
			row->AddCel(woxHeader, strWox);
		if(housingHeader)
			row->AddCel(housingHeader, strHousing);
		if(isQueenBeeHeader)
			row->AddCel(isQueenBeeHeader, strIsQueenBee);
		if(birthdayQueenBeeHeader)
			row->AddCel(birthdayQueenBeeHeader, strBirthDayQB);
		helper.GenerateUserCels(row, it1->second.m_nIdRecord);
		m_tableRegion.GetTableDrawer()->AddCelRow(row);
	}
	m_tableRegion.GetTableDrawer()->SortColumn(TABLE_FAMILY_NAME_ID);
}

void CViewFamilies::SetManipulation(Journal::typeItemMnpl type, int nId){
	IWnd* pWnd = IVisualManager::GetVisualManager()->
		GetVisualComponent(ILanguageManager::GetLanguageManager()->GetString(JOURNAL_CAPTION_ID))->GetComponentFrame();
	pWnd->Post(UM_ITEM_MANIPULATION, type, nId);
}

BEGIN_MESSAGE_MAP(CViewFamilies, CViewTable)
	ON_COMMAND(BUTTON_ADD_ID, OnAddFamily)
	ON_COMMAND(BUTTON_DELETE_ID, OnDeleteFamily)
	ON_COMMAND(BUTTON_CHANGE_ID, OnChangeFamily)
END_MESSAGE_MAP()

void CViewFamilies::OnAddFamily(){
	SetManipulation(Journal::ADD_FAMILY, m_pState->m_nIdParameter);
}
void CViewFamilies::OnDeleteFamily(){
	CTableCelRow* pSelectRow = m_tableRegion.GetTableDrawer()->GetSelectRow();
	std::tstring strContent = ILanguageManager::GetLanguageManager()->GetString(QUESTION_DELETE_ID) +
		_T(" '") + pSelectRow->GetValueColumn(TABLE_FAMILY_NAME_ID) + _T("'.");
	if(MessageBox(strContent.c_str(), ILanguageManager::GetLanguageManager()->
		GetString(DELETE_FAMILY_ID).c_str(), MB_YESNO) == IDYES){
			SetManipulation(Journal::DELETE_FAMILY, pSelectRow->GetId());
			m_tableRegion.GetTableDrawer()->DeleteRow(pSelectRow);
			Invalidate(FALSE);
	}
}
void CViewFamilies::OnChangeFamily(){
	SetManipulation(Journal::CHANGE_FAMILY, m_tableRegion.GetTableDrawer()->GetSelectRow()->GetId());
}

void CViewFamilies::EventHandler(){
	Init();
	if(IsCreate())
		Invalidate(FALSE);
}
